using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using RPGOnline.Extensions;
using RPGOnline.Properties;
using RPGOnline.Rendering;
using Light = RPGOnline.Controls.Map.Layers.Lighting.Light;
using LightsCollection = RPGOnline.Controls.Map.Layers.Lighting.LightsCollection;
using Matrix = Microsoft.DirectX.Matrix;

namespace RPGOnline.Controls.Map.Layers
{
    public class LightingLayer : ILayer
    {
		private readonly Renderer m_renderer;
        private readonly Map m_map;
        private LightsCollection m_lights;
        private Texture m_mapStatesTexture;
        private bool m_showObstructions;
        private SolidBrush m_obstructionsBrush;
        private SolidBrush m_clearBrush;
        private bool m_showLights;
        private Image m_lightImage;
        private bool m_mapStatesTextureInvalid = true;

        public LightingLayer(Renderer renderer, Map map, LightsCollection lightsCollection)
        {
            m_lightImage = Resources.Light;
            m_obstructionsBrush = new SolidBrush(Color.FromArgb(128, Color.Red));
            m_clearBrush = new SolidBrush(Color.FromArgb(200, Color.Black));
            m_renderer = renderer;
            m_map = map;
            m_lights = lightsCollection;
        }

        public LightsCollection Lights
        {
            get { return m_lights; }
            set { m_lights = value; }
        }

        public void Clear()
        {
        }

	    public void Update()
	    {
		    foreach (Light light in m_lights)
		    {
			    light.UpdateOcclusionMap(m_renderer);
		    }
	    }

        public void Draw()
        {
			foreach (Light light in m_lights)
	        {
				light.Draw(m_renderer);
	        }
        }

        public void ShowObstructions(bool enabled)
        {
            m_showObstructions = enabled;
        }

        public void ShowLights(bool enabled)
        {
            m_showLights = enabled;
        }

	    public void DrawPlaceholders()
	    {
			foreach (Light light in m_lights)
			{
				light.DrawPlaceholder(m_renderer);
			}
	    }
    }
}